# from PyPDF2 import PdfReader
#
# file_path = r"D:\yuanbei\data\解密文件\4.pdf"
#
# try:
#     reader = PdfReader(file_path)
#     if reader.is_encrypted:
#         print("PDF 被加密，需要密码")
#     else:
#         print("PDF 可以直接打开，不需要解密")
#         # 可以直接读取内容
#         text = ""
#         for page in reader.pages:
#             text += page.extract_text()
#         print(text[:500])  # 打印前500字符
# except Exception as e:
#     print("无法打开 PDF:", e)




# # 示例：读取文本文件
# file_path = r"D:\yuanbei\data\解密文件\4.pdf"
#
# with open(file_path, "r", encoding="utf-8") as f:
#     content = f.read()
#
# print(content)
#
# from PyPDF2 import PdfReader
#
# file_path = r"D:\yuanbei\data\解密文件\4.pdf"
# reader = PdfReader(file_path)
#
# text = ""
# for page in reader.pages:
#     text += page.extract_text()
#
# print(text[:500])  # 打印前500字符

from PyPDF2 import PdfReader

file_path = r"D:\yuanbei\data\解密文件\4.pdf"

try:
    reader = PdfReader(file_path)

    # 读取前500字符作为示例
    text = ""
    for page in reader.pages:
        text += page.extract_text()


    if text !="":

    print(text[:500])

    # 判断是否加密
    if reader.is_encrypted:
        print("PDF 被加密，需要密码，无法直接打开")
    else:
        print("PDF 可以直接打开")
        # 读取前500字符作为示例
        text = ""
        for page in reader.pages:
            text += page.extract_text()
        print(text[:500])

except FileNotFoundError:
    print("文件不存在")
except Exception as e:
    print("PDF 无法打开，错误:", e)



